package com.xhtt.modules.app.controller.cfg;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.exception.RRExceptionEnum;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.ExportExcel;
import com.xhtt.common.utils.PageUtils;
import com.xhtt.common.utils.R;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.cfg.entity.CfgBerthInfo;
import com.xhtt.modules.cfg.entity.CfgBerthRelate;
import com.xhtt.modules.cfg.model.ValidBerthParam;
import com.xhtt.modules.cfg.service.CfgBerthInfoService;
import com.xhtt.modules.cfg.service.CfgBerthRelateService;
import com.xhtt.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 泊位库 前端控制器
 * </p>
 *
 * @author feipc
 * @since 2019-02-21
 */
@Api(tags = "任务平台-基础数据维护-泊位库列表", value = "任务平台-基础数据维护-泊位库列表")
@RestController
@RequestMapping("app/sys/cfg/berth")
public class CfgBerthInfoController extends AbstractController {

    @Autowired
    private CfgBerthInfoService cfgBerthInfoService;
    @Autowired
    private CfgBerthRelateService berthRelateService;



    /**
     * 获取泊位库列表
     */
    @GetMapping(value = "/list")
    @ApiOperation("获取泊位库列表(web/app)")
    @Login
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = cfgBerthInfoService.queryPage(params);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 新增泊位库
     */
    @PostMapping(value = "/add")
    @ApiOperation("新增泊位库(web)")
    @Login
    public R add(@RequestBody CfgBerthInfo cfgBerthInfo) {
        cfgBerthInfoService.addBerth(cfgBerthInfo);
        return R.ok();
    }

    /**
     * 删除泊位库
     */
    @PostMapping(value = "/delete")
    @ApiOperation("删除泊位库(web)")
    @Login
    public R delete(@RequestBody Integer[] ids) {
        //假删除
        CfgBerthInfo p = new CfgBerthInfo();
        p.setStatus(-1);
        cfgBerthInfoService.update(p, new UpdateWrapper<CfgBerthInfo>().in(CfgBerthInfo.ID, Arrays.asList(ids)));
        return R.ok();
    }

    /**
     * 修改泊位库
     */
    @PostMapping(value = "/update")
    @ApiOperation("修改泊位库(web)")
    @Login
    public R update(@RequestBody CfgBerthInfo cfgBerthInfo) {
        cfgBerthInfoService.updateBerth(cfgBerthInfo);
        return R.ok();
    }

    /**
     * 获取泊位库
     */
    @GetMapping(value = "/get/{id}")
    @ApiOperation("获取泊位库(web/app)")
    @Login
    public R get(@PathVariable("id") Integer id) {
        CfgBerthInfo berth = cfgBerthInfoService.getById(id);
        return R.ok().put(Constant.DATA, berth);
    }

    /**
     * 查看泊位连接点
     */
    @GetMapping(value = "/getRelateObject")
    @ApiOperation("查看泊位连接点(web/app)")
    @Login
    public R getRelateObject(@RequestParam Integer berthId) {
        List<CfgBerthRelate> relates = cfgBerthInfoService.getRelateObject(berthId);
        return R.ok().put(Constant.DATA, relates);
    }

    /**
     * 查看泊位可用连接点
     */
    @GetMapping(value = "/getValidRelateObject")
    @ApiOperation("查看泊位可用连接点(web)")
    @Login
    public R getValidRelateObject(ValidBerthParam param) {
        List<CfgBerthRelate> relates = berthRelateService.getValidRelateObject(param);
        List<Integer> selectedPumpIds = param.getSelectedPumpIds();
        List<Integer> selectedTubeIds = param.getSelectedTubeIds();
        if (CollectionUtils.isNotEmpty(selectedPumpIds)) {
            Iterator<CfgBerthRelate> it = relates.iterator();
            while (it.hasNext()) {
                CfgBerthRelate r = it.next();
                if (3 == r.getType().intValue() && selectedPumpIds.contains(r.getRelateId())) {
                    it.remove();
                }
            }
        }
        if (CollectionUtils.isNotEmpty(selectedTubeIds)) {
            Iterator<CfgBerthRelate> it = relates.iterator();
            while (it.hasNext()) {
                CfgBerthRelate r = it.next();
                if (2 == r.getType().intValue() && selectedTubeIds.contains(r.getRelateId())) {
                    it.remove();
                }
            }
        }
        return R.ok().put(Constant.DATA, relates);
    }

    /**
     * 修改泊位连接点
     */
    @PostMapping(value = "/updateRelateObject")
    @ApiOperation("修改泊位连接点(web)")
    @Login
    public R updateRelateObject(@RequestBody List<CfgBerthRelate> relates) {
        cfgBerthInfoService.updateRelateObject(relates);
        return R.ok();
    }

    @GetMapping(value = "/export")
    @ApiOperation("泊位导出")
    @Login
    public void export(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        ExportExcel exportExcel = new ExportExcel();
        PageUtils page = cfgBerthInfoService.queryPage(params);
        List list = page.getList();
        String fileName = "泊位数据";
        String sheetName = "sheet1";
        String[] headers = {"泊位编号", "位置描述", "备注"};
        String[] columns = {"numberNo", "position", "memo"};
        try {
            exportExcel.exportExcel(fileName, sheetName, headers, columns, list, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(RRExceptionEnum.EXPORT_FAIL);
        }

    }

}
